package net.freebytes.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import java.util.Date;

/**
 * @description:
 * @author: lin
 * @time: 2024/12/18 14:22
 */
public class TestInitalizer extends ChannelInitializer<NioSocketChannel> {

    public TestInitalizer() {
        System.out.println(this);
    }

    @Override
    protected void initChannel(NioSocketChannel ch) throws Exception {
        // 解码
        ch.pipeline().addLast(new StringDecoder());

        ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
            @Override
            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                System.out.println(ctx.channel() + ",hello world");
            }

            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                System.out.println(new Date() + "：" + msg);
            }
        });
    }
}
